// pages/dizhi/dizhi.js
const app=getApp();
//获得全局js变量
var util = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgurl:app.data.imgurl,
        adderssinfo:[],
        isfrombuy:false,//来自购买页面
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.from){
            this.setData({
                isfrombuy:true
            })
            wx.removeStorageSync('changeaddressid')
        }
    },
    //选择地址
    changeaddress:function(e){
        var that = this
        var i = e.currentTarget.dataset.index
        var id = e.currentTarget.dataset.id
        //返回前一页---立即购买页面
        wx.setStorageSync('changeaddressid', id);
        wx.navigateBack({
            delta: 1
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        //获取地址信息
        this.getaddress();
    },
    getaddress:function(){
        var userid=app.globalData.userId;
        var that = this
        util.httpRrquest(app.data.rooturl + 'user/getUserAddress', 'GET', {userid:userid})
            .then(function (response) {
                that.setData({
                    adderssinfo: response.data.data,
                });
            }, function (error) {
                console.log(error);
            });
    },
    //设置默认地址
    setaddress(e) {
        var that = this
        var i = e.currentTarget.dataset.index
        var id = e.currentTarget.dataset.id
        var userid=app.globalData.userId;
        wx.showModal({
            title: '提示',
            content: '确定要设置此地址为默认地址？',
            success(res) {
                if (res.confirm) {
                    console.log('用户点击确定')
                    util.httpRrquest(app.data.rooturl + 'user/setAddress', 'GET', { id: id,userid:userid })
                        .then(function (response) {
                            wx.showToast({
                                title: '默认地址设置成功...',
                                icon: 'none'
                            })
                            that.setData({
                                adderssinfo: response.data.data,
                            })
                        }, function (error) {
                            console.log(error);
                        });
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },
    //删除地址
    deladdress(e) {
        var that = this
        var i = e.currentTarget.dataset.index
        var id = e.currentTarget.dataset.id
        var userid=app.globalData.userId;
        wx.showModal({
            title: '提示',
            content: '确定要删除此地址？',
            success(res) {
                if (res.confirm) {
                    console.log('用户点击确定')
                    var lists = that.data.adderssinfo
                    var arr = lists.filter((item, index) => index != i);
                    that.setData({
                        adderssinfo: arr
                    })
                    util.httpRrquest(app.data.rooturl + 'user/delAddress', 'GET', { id: id,userid:userid })
                        .then(function (response) {
                            wx.showToast({
                                title: '删除地址成功...',
                                icon: 'none'
                            })
                        }, function (error) {
                            console.log(error);
                        });
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },
    //编辑地址
    editaddress:function(e){
        var i = e.currentTarget.dataset.index
        var id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '/pages/editaddress/editaddress?id='+id,
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        var shareinfo = app.setShareMessage();
        console.log(shareinfo);
        return {
            title: shareinfo.title,
            path: shareinfo.path,
            imageUrl: shareinfo.imageUrl,
            success: (res) => {
                wx.showToast({
                    title: "分享成功",
                    icon: 'loading'
                })
            }
        }
    }
})